package com.moon.interface_api.utils;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

/**
 *Created by chengliang on 2019/3/4.
 * 输出流，输入流切换   string
 */
public class IOUtil {    
    
    //inputStream转outputStream
    public ByteArrayOutputStream parse(InputStream in) throws Exception{
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();    
        int ch;       
        while ((ch = in.read()) != -1) {   
            swapStream.write(ch);   
        }      
        return swapStream;
    }    
    
    
    //outputStream转inputStream
    public ByteArrayInputStream parse(OutputStream out) throws Exception{
        ByteArrayOutputStream  baos = new   ByteArrayOutputStream();
        baos=(ByteArrayOutputStream) out;
        ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());      
        return swapStream;
    }    
    
    
    //inputStream转String
    public static String parse_String(InputStream in) throws Exception{
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        int ch;
        while ((ch = in.read()) != -1) {   
            swapStream.write(ch);   
        }
        return swapStream.toString();
    }    
    
    
    
    //OutputStream 转String
    public String parse_String(OutputStream out)throws Exception{
        ByteArrayOutputStream  baos = new ByteArrayOutputStream();
        baos=(ByteArrayOutputStream) out;
        ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
        return swapStream.toString();
    }    
    
    
    /**
     *@Author:chengliang
     *@date 2019/3/4_17:24
     *@param
     *@return
     * String转inputStream
     */
    public ByteArrayInputStream parse_inputStream(String in)throws Exception{
        ByteArrayInputStream input=new ByteArrayInputStream(in.getBytes());
        return input;
    }    
    
    
    /**
     *@Author:chengliang
     *@date 2019/3/4_17:24
     *@param 
     *@return 
     * String 转outputStream
     */
    public ByteArrayOutputStream parse_outputStream(String in)throws Exception{ 
        return parse(parse_inputStream(in));
    }



    public void inputstreamtofile(InputStream ins,File file) throws Exception {
        OutputStream os = new FileOutputStream(file);
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.close();
        ins.close();
    }

        public static void main(String[] args) {
        String s1 = HttpUtils.openTxtFile("http://jinjumanhua.oss-cn-shanghai.aliyuncs.com/novel/测试/测试_subs.txt");
        System.out.println(s1);

        try {
            URL url = new URL("http://jinjumanhua.oss-cn-shanghai.aliyuncs.com/novel/测试/测试_subs.txt");
            URLConnection conn = url.openConnection();
            InputStream in = conn.getInputStream();
            
            ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
            int ch;
            while ((ch = in.read()) != -1) {
                swapStream.write(ch);
            }
            
            System.out.println(swapStream.toString());
            System.out.println();
            in.close();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            
        }
    }

  
}